package com.swak.frame.aware;

import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import com.swak.frame.logger.SwakLoggerFactory;

@Component
public class InjectBeanSelfProcessor implements SwakPostProcessor {

    private Logger logger = SwakLoggerFactory.getLogger(InjectBeanSelfProcessor.class);

    @Override
    public void postBeanAfterInitialization(Object bean, String beanName) {
        // 因为spring的aop事物管理存在嵌套调用问题，故需要如此处理
        if (BeanSelfAware.class.isAssignableFrom(bean.getClass())) {
            logger.warn("injectBeanSelfProcessor.setSelf. beanName:{}", bean.getClass().getName(),
                beanName);
            @SuppressWarnings({"rawtypes", "unchecked"})
            BeanSelfAware<Object> bs = ((BeanSelfAware) bean);
            bs.setServiceBeanSelf(bean);
        }
    }
}
